<link rel="stylesheet" type="text/css" href="<?php echo COMPONENT_PATH."css/datepicker.css"; ?>" />
<?php 
global $managers;
//echo $sql;
//print_r($_POST);
?>

<form method="post" action="" name="filter_form">
<table width="100%" border="0">
  <tr class="HeaderTr">
    <td colspan="4"><b>Reservations (<a href="javascript:void(0);" class="Filter">Filter</a>)</b></td>
  </tr>
  <tr>
  	<td>
  		<table id="FilterTable">
  			<tr>
			    <td width="10%"></td>
			    <td width="50%"></td>
			    <td width="20%"></td>
			    <td align="right">
			    	<input type="submit" value="Apply" />
			    	<input type="hidden" name="form" value="filter">
			    	<input type="hidden" name="set_confirmed" value="1">
			    	<input type="reset" value="Reset" />
			    </td>
			  </tr>
			  <tr>
			    <td width="230">Unit Address</td>
			    <td>
			    	<select name="unit_address">
			    	<option value="">Select</option>
			    	<?php foreach($units as $unit){?>
			    		<option value="<?php echo $unit->id;?>"><?php echo $unit->address;?></option>
			    	<?php }?>
			    	</select>
			    </td>
			    <td>Booking Ref</td>
			    <td><input type="text" size="10" name="booking_reference" /></td>
			  </tr>
			  <tr>
			    <td>Start Date</td>
			    <td><input type="text" id="arr_date" name="arr_date" class="date_pick" /></td>
			    <td>Unit Manager</td>
			    <td>
				    <select name="unit_manager">
				    <option value="">Select</option>
				    <?php foreach($managers as $manager){?>
				    	<option value="<?php echo $manager->id;?>"><?php echo $manager->title." ".$manager->firstname." ".$manager->surname;?></option>
				    <?php }?>
				    </select>
			    </td>
			  </tr>
			  <tr>
			    <td>End Date</td>
			    <td><input type="text" id="dep_date" name="dep_date" class="date_pick" /></td>
			    <td>Customer Surname</td>
			    <td><input type="text" name="surname" /></td>
			  </tr>
			  <tr>
			    <td>Booked On</td>
			    <td><input type="text" name="booked_on" class="date_pick" /></td>
			    <td>Email</td>
			    <td><input type="text" name="email" /></td>
			  </tr>
			  <tr>
			    <td>confirmed</td>
			    <td>
			    	<select name="confirmed">
			    		<option value="">Select</option>
			    		<option value="1">Yes</option>
			    		<option value="0">No</option>
			    	</select>
			    </td>
			    <td>Deposit Paid</td>
			    <td>
			    	<select name="deposit_paid">
			    		<option value="">Select</option>
			    		<option value="1">Yes</option>
			    		<option value="0">No</option>
			    	</select>
			    </td>
			  </tr>
			  <tr>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td>Paid</td>
			    <td>
			    	<select name="paid">
			    		<option value="">Select</option>
			    		<option value="1">Yes</option>
			    		<option value="0">No</option>
			    	</select>
			    </td>
			  </tr>
			  <tr>
			    <td></td>
			    <td></td>
			    <td>Security Deposit Received</td>
			    <td>
			    	<select name="sd_received">
			    		<option value="">Select</option>
			    		<option value="1">Yes</option>
			    		<option value="0">No</option>
			    	</select>
			    </td>
			  </tr>
			  <tr>
			    <td></td>
			    <td></td>
			    <td>Security Deposit Returned</td>
			    <td>
			    	<select name="sd_returned">
			    		<option value="">Select</option>
			    		<option value="1">Yes</option>
			    		<option value="0">No</option>
			    	</select>
			    </td>
			  </tr>
		</table>
   	</td>
  </tr>
  <tr>
    <td>
		<table id="ReservationList" class="Centered List" width="100%" border="0">
		  <tr>
		    <th colspan="14" class="LeftAlign"><h2>Reservation Information</h2></th>
		  </tr>
		  <tr class="BottomBordered">
		    <th width="15px"><input type="checkbox" name="" onclick="select_all(this);" /></th>
		    <th>Booking Ref</th>
		    <th>Address</th>
		    <th>Guest Name</th>
		    <th>Guest Email</th>
		    <th>Arrival</th>
		    <th>Departure</th>
		    <th>Confirmed</th>
		    <th>Dep</th>
		    <th>Bal</th>
		    <th>SD IN</th>
		    <th>SD OUT</th>
		    <th>OC Dep</th>
		    <th>OC Bal</th>
		  </tr>
		<?php 
		if(!empty($bookings)){
			foreach($bookings as $key=>$booking){
			$guest = $booking->customers;
			$unit = $booking->units;
		?>
		  <tr class="<?php echo($key%2 == 0 ? 'row0' : 'row1'); ?>">
		    <td><input type="checkbox" name="bookings[]" value="<?php echo $booking->id;?>" /></td>
		    <td><a href="?option=<?php echo COMPONENT_NAME;?>&page=bookings&action=edit&bid=<?php echo $booking->id;?>"><?php echo $booking->id; ?></a></td>
		    <td><?php echo $unit->address; ?></td>
		    <td><?php echo $guest->firstname." ".$guest->surname; ?></td>
		    <td><a href="mailto:<?php echo $guest->email;?>"><?php echo $guest->email?></a></td>
		    <td><?php echo get_string_from_date($booking->startdate)?></td>
		    <td><?php echo get_string_from_date($booking->enddate)?></td>
		    <td>
	    		<a class="Confirmed" style="display: <?php echo($booking->confirmed == 1 ? 'inline':'none')?>;" href="javascript:void(0);" onclick="alter_confirmation_state(0, <?php echo $booking->id; ?>, this)"><img src="<?php echo COMPONENT_PATH;?>images/published.png" /></a>
	    		<a class="UnConfirmed" style="display: <?php echo($booking->confirmed != 1 ? 'inline':'none')?>;" href="javascript:void(0);" onclick="alter_confirmation_state(1, <?php echo $booking->id; ?>, this)"><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    </td>
		    <td>
		    	<?php if($booking->total_paid != 0 ){ ?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/tick.png" /></a>
		    	<?php }else{?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    	<?php }?>
		    </td>
		    <td>
		    	<?php if($booking->total_paid == total_price ){ ?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/tick.png" /></a>
		    	<?php }else{?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    	<?php }?>
		    </td>
		    <td>
		    	<?php if($booking->security_deposit_received_number != "" ){ ?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/tick.png" /></a>
		    	<?php }else{?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    	<?php }?>
		    </td>
		    <td>
		    	<?php if($booking->security_deposit_returned_number != "" ){ ?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/tick.png" /></a>
		    	<?php }else{?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    	<?php }?>
		    </td>
		    <td>
		    	<?php if($booking->deposit_sent_to_owner_number != "" ){ ?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/tick.png" /></a>
		    	<?php }else{?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    	<?php }?>
		    </td>
		    <td>
		    	<?php if($booking->balance_sent_to_owner_number != "" ){ ?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/tick.png" /></a>
		    	<?php }else{?>
		    	<a><img src="<?php echo COMPONENT_PATH;?>images/cross.png" /></a>
		    	<?php }?>
		    </td>
		  </tr>
		<?php }?>
		<tr class="HeaderTr">
			<td colspan="6" class="LeftAlign">
				<?php if( $paginate['pg_no'] == 1 ){?>
					&lt;&lt;Start&nbsp;&nbsp;&lt;Previous&nbsp;
				<?php }else{?>
					<a href="?option=<?php echo COMPONENT_NAME;?>&page=<?php echo $_GET['page']?>&pg_no=1&limit=<?php echo $paginate['limit'];?>">&lt;&lt;Start</a>&nbsp;&nbsp;<a href="?option=<?php echo COMPONENT_NAME;?>&page=<?php echo $_GET['page']?>&pg_no=<?php echo $paginate['prev'];?>&limit=<?php echo $paginate['limit'];?>">&lt;Previous</a>
				<?php }?>
				<?php for($i = 1; $i <= $paginate['max_page']; $i++){?>
					<?php if( $i == $paginate['pg_no'] ){?>
						<?php echo $i;?>
					<?php }else{?>
						&nbsp;&nbsp;<a href="?option=<?php echo COMPONENT_NAME;?>&page=<?php echo $_GET['page']?>&pg_no=<?php echo $i;?>&limit=<?php echo $paginate['limit'];?>"><?php echo $i;?></a>&nbsp;&nbsp;
					<?php }?>
				<?php }?>
				<?php if($paginate['pg_no'] == $paginate['max_page']){?>
					&nbsp;Next&gt;&nbsp;&nbsp;End&gt;&gt;
				<?php }else{?>
					<a href="?option=<?php echo COMPONENT_NAME;?>&page=<?php echo $_GET['page']?>&pg_no=<?php echo $paginate['next'];?>&limit=<?php echo $paginate['limit'];?>">Next&gt;</a>&nbsp;&nbsp;<a href="?option=<?php echo COMPONENT_NAME;?>&page=<?php echo $_GET['page']?>&pg_no=<?php echo $paginate['max_page'];?>&limit=<?php echo $paginate['limit'];?>">End&gt;&gt;</a>
				<?php }?>
			</td>
		</tr>
		<?php }else{?>
		<tr><td colspan="13" class="Info LeftAlign"><b>No results found for your query</b></td></tr>
		<?php }?>
		  <tr>
		  	<td align="center" colspan="13">
		  		Display 
		  		<select onchange="document.filter_form.submit()" size="1" name="limit">
					<option value="5" <?php echo ($limit == 5 ? 'selected = "selected"' : ''); ?>>5</option>
					<option value="10" <?php echo ($limit == 10 ? 'selected = "selected"' : ''); ?>>10</option>
					<option value="15" <?php echo ($limit == 15 ? 'selected = "selected"' : ''); ?>>15</option>
					<option value="20" <?php echo ($limit == 20 ? 'selected = "selected"' : ''); ?>>20</option>
					<option value="25" <?php echo ($limit == 25 ? 'selected = "selected"' : ''); ?>>25</option>
					<option value="30" <?php echo ($limit == 30 ? 'selected = "selected"' : ( empty($limit) ? 'selected = "selected"' : '' )); ?>>30</option>
					<option value="50" <?php echo ($limit == 50 ? 'selected = "selected"' : ''); ?>>50</option>
				</select>
				rows
		  	</td>
		  </tr>
		</table>
	</td>
  </tr>
</table>
</form>
<?php $KToolBar->addLink('<a href="?option='.COMPONENT_NAME.'&page=bookings&action=new"><img border="0" alt="New" src="images/new_f2.png"/><br />New</a>');?>
<?php $KToolBar->addLink('<a href="#" onclick="delete_selected(\'filter_form\')"><img border="0" alt="Delete" src="images/delete_f2.png"/><br />Delete</a>');?>
<?php $KToolBar->addLink('<a href="#" onclick="set_confirmed(\'filter_form\', 1)"><img border="0" alt="Confirm" src="images/apply_f2.png"/><br />Confirm</a>');?>
<?php $KToolBar->addLink('<a href="#" onclick="set_confirmed(\'filter_form\', 0)"><img border="0" alt="UnConfirm" src="images/cancel_f2.png"/><br />Unconfirm</a>');?>
<script type="text/javascript" src="<?php echo COMPONENT_PATH."js/date.js"; ?>" charset="ISO-8859-1"></script>
<script type="text/javascript" src="<?php echo COMPONENT_PATH."js/jquery.datePicker.js"; ?>" charset="ISO-8859-1"></script>
<script type="text/javascript">
alternate("ReservationList", "Row0", "Row1");
Date.firstDayOfWeek = 0;
Date.format = 'mm/dd/yyyy';
$(function(){
	$('.date_pick').datePicker();
	$('#arr_date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#dep_date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#dep_date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#arr_date').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
	$(".Filter").click(function(){
		$("#FilterTable").slideToggle("slow");
	});
});
function alter_confirmation_state(state, bid, ele){
	if(ele.className == "Confirmed") var index = $(".Confirmed").index(ele);
	else if(ele.className == "UnConfirmed") var index = $(".UnConfirmed").index(ele);
	$.post("<?php echo COMPONENT_PATH."functions/change_confirmation.php";?>", { confirmed: state, id: bid },function(data){
		if(data == 1) {
			$(".UnConfirmed:eq("+index+")").css("display", "none");
			$(".Confirmed:eq("+index+")").css("display", "inline");
		}
		else if(data == 0) {
			$(".Confirmed:eq("+index+")").css("display", "none");
			$(".UnConfirmed:eq("+index+")").css("display", "inline");
		}
		else alert("Error while changing the confirmation. Make sure you are connected to the network.");
	});
}
</script>